package utils

func SwapBetween(source []byte, i, j int) {
	for i < j {
		var temp = source[i]
		source[i] = source[j]
		source[j] = temp
		i++
		j--
	}
}

func NewSlice(size int, initVal any) []any {
	res := make([]any, size)
	if initVal == 0 {
		return res
	}
	for i := 0; i < size; i++ {
		res[i] = initVal
	}
	return res
}

func NewSlice2Dimension(row, col int, initVal any) [][]any {
	res := make([][]any, row)
	if initVal == 0 {
		return res
	}

	for i := 0; i < row; i++ {
		curCol := make([]any, col)
		for j := 0; j < col; j++ {
			curCol[j] = initVal
		}
		res[i] = curCol
	}
	return res
}

// 定义一个新的类型：element
type element interface {
	// element 支持如下类型
	string | int8 | int16 | int32 | int64 | int | float32 | float64 | uint | uint8 | uint16 | uint32 | uint64
}

// Unique 数组或slice边去重
func Unique[T element](arr []T) []T {
	tmp := make(map[T]struct{})
	l := len(arr)
	if l == 0 {
		return arr
	}

	rel := make([]T, 0, l)
	for _, item := range arr {
		_, ok := tmp[item]
		if ok {
			continue
		}
		tmp[item] = struct{}{}
		rel = append(rel, item)
	}

	return rel[:len(tmp)]
}
